package com.aitoedu.server.controller.teacher;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.aitoedu.server.dto.HomeworkDTO;
import com.aitoedu.server.dto.PreviewDTO;
import com.aitoedu.server.dto.SetDTO;
import com.aitoedu.server.entity.Course;
import com.aitoedu.server.service.CourseService;
import com.aitoedu.server.service.PreviewService;
import com.aitoedu.server.service.TtestbankService;
import com.aitoedu.server.vo.CourseCreateVO;
import com.aitoedu.server.vo.QuestionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher/course")
@Slf4j
@CrossOrigin
@Tag(name = "课程相关接口")
@SaCheckRole("教师")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TtestbankService ttestbankService;

    @Autowired
    private PreviewService previewService;

    @GetMapping("/create")
    @Operation(summary = "增加课程(创建班级)")
    public SaResult create(String courseName){
        CourseCreateVO courseCreateVO= courseService.createCourse(courseName);
        return SaResult.ok().setData(courseCreateVO);
    }

    @PostMapping("/import")
    @Operation(summary = "从官方题库导入题目到教师题库")
    public SaResult importQuestion(@RequestBody List<Long> ids){
        ttestbankService.importQuestion(ids);
        this.deleteByKey("TTestBank:");
        return SaResult.ok();
    }

    @PostMapping("/set")
    @Operation(summary = "教师自建题目")
    public SaResult set(@RequestBody List<SetDTO> setDTOList){
        ttestbankService.set(setDTOList);
        this.deleteByKey("TTestBank:");
        return SaResult.ok();
    }

    @PostMapping("/homework")
    @Operation(summary = "发布作业")
    public SaResult homework(@RequestBody HomeworkDTO homeworkDTO){
        ttestbankService.homework(homeworkDTO);
        return SaResult.ok();
    }

    @PostMapping("/preview")
    @Operation(summary = "发布预习内容")
    public SaResult preview(@RequestBody PreviewDTO previewDTO){
        previewService.pushPreview(previewDTO);
        return SaResult.ok();
    }

    @GetMapping("/myCourse")
    @Operation(summary = "查询所教课程")
    public SaResult myCourse(){
        List<Course> courses = courseService.myCourse();
        return SaResult.ok().setData(courses);
    }

    @GetMapping("/allQuestion")
    @Operation(summary = "教师查询自己的题库")
    public SaResult allQuestion(){
//        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
//        List<QuestionVO> list = (List<QuestionVO>) redisTemplate.opsForValue().get("TTestBank:"+tid);
//        if(list != null) return SaResult.ok().setData(list);
        List<QuestionVO> list = ttestbankService.allQuestion();
//        redisTemplate.opsForValue().set("TTestBank:"+tid,list);
        return SaResult.ok().setData(list);
    }
    private void deleteByKey(String key){
        Long id = Long.valueOf(StpUtil.getLoginId().toString());
        redisTemplate.delete(key+id);
    }
    private void setByKey(String key,Object obj){
        Long id = Long.valueOf(StpUtil.getLoginId().toString());
        redisTemplate.opsForValue().set(key+id,obj);
    }

}
